package com.rongcheng.frc.newstyledemo.activity.login;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.rongcheng.frc.androidlib.net.RequestCallback;
import com.rongcheng.frc.androidlib.net.RequestParameter;
import com.rongcheng.frc.newstyledemo.R;
import com.rongcheng.frc.newstyledemo.activity.personcenter.PersonCenterActivity;
import com.rongcheng.frc.newstyledemo.base.AppBaseActivity;
import com.rongcheng.frc.newstyledemo.engine.AppConstants;
import com.rongcheng.frc.newstyledemo.engine.RemoteService;
import com.rongcheng.frc.newstyledemo.engine.User;
import com.rongcheng.frc.newstyledemo.entity.UserInfo;

public class LoginActivity extends AppBaseActivity {
    private Button btnLogin;
    private EditText mEdtName, mEdtPwd;


    @Override
    protected void initVariables() {
        Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;

        needCallback = bundle.getBoolean(AppConstants.NeedCallback, false);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);

        mEdtName = (EditText) findViewById(R.id.email);
        mEdtPwd = (EditText) findViewById(R.id.password);
        //登录事件
        btnLogin = (Button) findViewById(R.id.sign_in_button);
        YXDlg = new ProgressDialog(LoginActivity.this);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //showDlg();

                login();
            }
        });


    }

    @Override
    protected void loadData() {

    }

    private void login() {

        RequestCallback loginCallback = new AbstractRequestCallback() {

            @Override
            public void onSuccess(String content, boolean isMock) {

                Log.e("frc", content);
                Log.e("frc", "mock is " + isMock);


                    UserInfo userInfo = JSON.parseObject(content,
                            UserInfo.class);
                    Log.e("frc", userInfo.getLoginName());
                    Log.e("frc", userInfo.getUserName());
                    if (userInfo != null) {
                        User.getInstance().reset();
                        User.getInstance().setLoginName(userInfo.getLoginName());
                        User.getInstance().setScore(userInfo.getScore());
                        User.getInstance().setUserName(userInfo.getUserName());
                        User.getInstance().setLoginStatus(true);
                        User.getInstance().save();
                    }


                if (needCallback) {
                    setResult(Activity.RESULT_OK);
                    finish();
                } else {
                    Intent intent = new Intent(LoginActivity.this,
                            PersonCenterActivity.class);
                    startActivity(intent);
                }
            }


        };

        ArrayList<RequestParameter> params = new ArrayList<RequestParameter>();
        RequestParameter rp1 = new RequestParameter("loginName", mEdtName.getText().toString());
        RequestParameter rp2 = new RequestParameter("password", mEdtPwd.getText().toString());
        params.add(rp1);
        params.add(rp2);

        RemoteService.getInstance().invoke(this, "login", params,
                loginCallback);


    }


}